﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Admin_Components_SaleOrder_SaleOrderList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadDataForm();
        }
    }

    private void loadDataForm()
    {
       
        //Load tinh thanh
        int ProvinceID = Session["ProvinceID"] == null ? 0 : int.Parse(Session["ProvinceID"].ToString());
        int isProvinceAll = (ProvinceID == 0 ? 1 : 0);
        Utils.loadLocation(cboProvinceIDSearch, 0, isProvinceAll, ProvinceID);
        //load Shop
        int ShopID = Session["ShopID"] == null ? 0 : int.Parse(Session["ShopID"].ToString());
        int isShopAll = (ShopID == 0 ? 1 : 0);
        Utils.loadShopByProvinceToCombo(cboShop, isShopAll,ProvinceID, ShopID, "");

        Utils.loadEmployeeToCombo(cboEmployeeDeliveryCode, 3, ShopID, 1);
        Utils.loadEmployeeToCombo(cboEmployeeSale, 0, 0, 1);
        Utils.LoadCodeDetail(cboStatus, "SaleOderStatus", 1);
        Utils.LoadCodeDetail(cboSaleOrderType, "SaleOderType", 1);
        gv.DataSource = new DataTable();
        gv.DataBind();
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/SaleOrderList", true);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        gv.CurrentPageIndex = 0;
        LoadData();
        gv.DataBind();
    }

    protected void gvData_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        LoadData();
    }

    private void LoadData()
    {
        DBDataContext db = LINQUtil.GetDBWriteOnly();
        String Status = this.cboStatus.SelectedValue;
        String Shop = this.cboShop.SelectedValue;
        int ProvinceID = int.Parse(this.cboProvinceIDSearch.SelectedValue == "" ? "0" : this.cboProvinceIDSearch.SelectedValue);
        String SaleOrderCode = this.txtSaleOrderCode.Text.Trim();
        String Customer = this.txtCustomerName.Text.Trim();
        String EmployeeDelivery = this.cboEmployeeDeliveryCode.SelectedValue;//0
        String EmployeeSale = this.cboEmployeeSale.SelectedValue;
        String Product = this.txtProduct.Text.Trim();
        String PersonReceipt = this.txtReceiptPerson.Text.Trim();
        String PersonPhone = this.txtReceiptPhoneNumer.Text.Trim();
        String PersonAddress = this.txtReceiptAdress.Text.Trim();
        DateTime FromDate = new DateTime(2014, 06, 01);
        if (this.dtpFromDate.SelectedDate != null)
        {
           FromDate = this.dtpFromDate.SelectedDate.Value;
        }
        DateTime ToDate = new DateTime(2099, 12, 31);
        if (this.dtpToDate.SelectedDate != null)
        {
           ToDate= this.dtpToDate.SelectedDate.Value;
        }
        int PageNumber = gv.CurrentPageIndex + 1;
        int PageSize = gv.PageSize;
        String saleOrderType = this.cboSaleOrderType.SelectedValue;
        var q = db.SaleOrder_Search(SaleOrderCode, Status, ProvinceID, Shop, Customer, EmployeeDelivery, Product, PersonReceipt, PersonPhone, PersonAddress, FromDate, ToDate, PageSize, PageNumber, saleOrderType, EmployeeSale).ToList();
        if (q.Count > 0)
        {
            gv.Height = 250;
            gv.ClientSettings.Scrolling.AllowScroll = true;
            gv.ClientSettings.Scrolling.UseStaticHeaders = true;
            gv.VirtualItemCount = q[0].TotalRows.Value;
            gv.DataSource = q;
        }
        else
        {
            gv.DataSource = new DataTable();
        }
    }

    protected void gvData_ItemCommand(object sender, GridCommandEventArgs e)
    {
        string commandName = e.CommandName;

        if (commandName == "ViewItem")
        {
            try
            {
                /*
                GridDataItem item = e.Item as GridDataItem;
                int iRow = e.Item.ItemIndex;
                string pageurl = "PurchaseOrder";
                Session["purchaseID"] = item.GetDataKeyValue("ID"); ;
                ScriptManager.RegisterStartupScript(UpdatePanel1, Page.GetType(), "", "window.open('" + pageurl + "','_blank')", true);
                 */
                DBDataContext db = LINQUtil.GetDBWriteOnly();
                GridDataItem item = e.Item as GridDataItem;
                String SaleOrderID = e.CommandArgument.ToString();
                Session["SaleOrderID"] = SaleOrderID;
                Session["Task"] = "EditSO";
                var so = db.SaleOrders.FirstOrDefault<SaleOrder>(a => a.ID == int.Parse(SaleOrderID));
                if (so != null)
                {
                    if (so.SaleOderType == "K" || so.SaleOderType == "B")
                    {
                        ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('" + ResolveUrl("~/don-hang-ban-buon") + "');", true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('" + ResolveUrl("~/SaleOrder") + "');", true);
                    }
                }
                else
                {
                    JavascriptUtil.LoadMessage(UpdatePanel1, false, "Đơn hàng không tồn tại");
                    
                }
                
            }
            catch (Exception ex)
            {
                JavascriptUtil.LoadMessage(UpdatePanel1, false, "Có lỗi xảy ra!");
                ControlUtil.Log("----------------------------");
                ControlUtil.Log("SaleOrderList-gvData_ItemCommand");
                ControlUtil.Log(ex.ToString());
                ControlUtil.Log("----------------------------");
            }
        }
    }

    protected void cboProvinceIDSearch_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        int ProvinceID = int.Parse(this.cboProvinceIDSearch.SelectedValue == "" ? "0" : this.cboProvinceIDSearch.SelectedValue);
        Utils.loadShopByProvinceToCombo(cboShop, 1, ProvinceID,0, "");
    }
}